package com.demo.service.Impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.demo.beans.BasePojo;
import com.demo.beans.Bean;
import com.demo.mapper.demo.BeanMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class BeanServiceImpl {
    @Autowired
    private BeanMapper<BasePojo> beanMapper;
    public Bean getBeanById(String id) {
//        Bean bean = beanMapper.getBeanById(id);
        Bean bean = beanMapper.selectById(id);
        return bean;
    }

    @Transactional(rollbackFor = Exception.class)
    public boolean batchSave(List<Bean> beanList){
        int size = beanList.size();
        int count = 0;
        for (Bean bean : beanList) {
            Integer result = beanMapper.insert(bean);
            count = count + result;
            /*if (count == 88){
                int a = 1 / 0;
            }*/
        }
        if (size == count){
            return true;
        }
        return  false;
    }
    public List<Bean> qbByCondition(String beginYear, String endYear, String schoolName, String branch, String major, String zdfwc){
        String year = null;
        QueryWrapper<Bean> wrapper = new QueryWrapper<>();
        if (beginYear.length() > 0 && endYear.length() > 0){
            wrapper.between("year", beginYear, endYear);
        }else if (beginYear.length() > 0 && endYear.length() == 0){
            year = beginYear;
        }else if (endYear.length() > 0 && beginYear.length() == 0){
            year = endYear;
        }
        if (year != null){
            wrapper.eq("year",year);
        }
        if (!"".equals(branch)){
            wrapper.eq("branch",branch);
        }
        if (zdfwc.length() > 0){
            /*if (Integer.valueOf(zdfwc) > 0){
                wrapper.orderByDesc("zdfwc");
            }else {
                wrapper.orderByAsc("zdfwc");
            }*/
//            wrapper.orderByAsc("zdfwc");
            int abs = Math.abs(Integer.valueOf(zdfwc));
            wrapper.between("zdfwc",abs - 2000, abs + 2000);
            if (!"".equals(major)){
                wrapper.eq("major",major);
            }
        }else {
            if (schoolName.length() > 0){
                wrapper.like("school_name",schoolName);
            }
            if (!"".equals(major)){
                wrapper.eq("major",major);
            }
        }
        wrapper.orderByAsc("zdfwc");
        List<Bean> beanList = beanMapper.selectList(wrapper);
        return beanList;
    }

    public IPage<Bean> qbByCondition(String beginYear, String endYear, String schoolName, String branch, String major, String zdfwc, String pageIndex, String pageSize){
        String year = null;
        QueryWrapper<Bean> wrapper = new QueryWrapper<>();
        if (beginYear.length() > 0 && endYear.length() > 0){
            wrapper.between("year", beginYear, endYear);
        }else if (beginYear.length() > 0 && endYear.length() == 0){
            year = beginYear;
        }else if (endYear.length() > 0 && beginYear.length() == 0){
            year = endYear;
        }
        if (StringUtils.isNotEmpty(year)){
            wrapper.eq("year",year);
        }
        if (StringUtils.isNotEmpty(branch)){
            wrapper.eq("branch",branch);
        }
        if (zdfwc.length() > 0){
            /*if (Integer.valueOf(zdfwc) > 0){
                wrapper.orderByDesc("zdfwc");
            }else {
                wrapper.orderByAsc("zdfwc");
            }*/
//            wrapper.orderByAsc("zdfwc");
            int abs = Math.abs(Integer.valueOf(zdfwc));
            wrapper.between("zdfwc",abs - 2000, abs + 2000);
            if (!"".equals(major)){
                wrapper.eq("major",major);
            }
        }else {
            if (schoolName.length() > 0){
                wrapper.like("school_name",schoolName);
            }
            if (!"".equals(major)){
                wrapper.eq("major",major);
            }
        }
        wrapper.orderByAsc("zdfwc");
        /*pageIndex = "2";
        pageSize = "20";*/
        Page<Bean> page = new Page<>(Long.valueOf(pageIndex),Long.valueOf(pageSize));
        IPage<Bean> beanIPage = beanMapper.selectPage(page, wrapper);
//        List<Bean> beanList = beanMapper.selectList(wrapper);
        return beanIPage;
    }
    public boolean sureSaveBean(){
        boolean b = this.beanMapper.sureSave();
        if (b){
            return true;
        }
        return false;
    }
    public boolean rollbackBean(){
        QueryWrapper<Bean> wrapper = new QueryWrapper<>();
        wrapper.eq("is_new",1);
        Integer integer = this.beanMapper.selectCount(wrapper);
        int delete = this.beanMapper.delete(wrapper);
        if (delete == integer){
            return true;
        }
        return false;
    }
    public boolean deleteDatabase(){
        QueryWrapper<Bean> wrapper = new QueryWrapper<>();
        int delete = this.beanMapper.delete(wrapper);
        Integer integer = this.beanMapper.selectCount(wrapper);
        if (integer == 0){
            return true;
        }
        return false;
    }
}
